package com.mass.waybill.records.service.impl;

import com.mass.core.utils.AjaxResponse;
import com.mass.core.utils.NumberUtils;
import com.mass.waybill.order.dao.WbShippingOrderMapper;
import com.mass.waybill.order.model.WbShippingOrder;
import com.mass.waybill.order.model.WbShippingOrderExample;
import com.mass.waybill.order.service.WbShippingOrderService;
import com.mass.waybill.records.dao.WbSterilisationRecordsMapper;
import com.mass.waybill.records.model.WbSterilisationRecords;
import com.mass.waybill.records.model.WbSterilisationRecordsExample;
import com.mass.waybill.records.service.WbSterilisationRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: ZSX
 * @Description: 冲销记录  service 接口  实现类
 */
@Service
@Transactional
public class WbSterilisationRecordsServiceImpl implements WbSterilisationRecordsService {

    @Autowired
    private WbSterilisationRecordsMapper wbSterilisationRecordsMapper;
    @Autowired
    private WbShippingOrderService wbShippingOrderService;
    @Autowired
    private WbShippingOrderMapper wbShippingOrderMapper;

    @Override
    public AjaxResponse findReceivables(WbShippingOrder wbShippingOrder, Integer page, Integer limit) {
        List<WbShippingOrder> wbShippingOrderList = wbSterilisationRecordsMapper.findReceivables(wbShippingOrder, (page - 1) * limit, limit);
        long count = wbSterilisationRecordsMapper.findReceivablesCount(wbShippingOrder);
        return AjaxResponse.success("ok", wbShippingOrderList, count, page);
    }

    @Override
    public List<WbShippingOrder> findReceivablesAll(WbShippingOrder wbShippingOrder) {
        List<WbShippingOrder> wbShippingOrderList = wbSterilisationRecordsMapper.findReceivables(wbShippingOrder, null, 0);
        return wbShippingOrderList;
    }

    @Override
    public WbSterilisationRecords findBySettlementNumber(String settlementNumber) {
        WbSterilisationRecordsExample example = new WbSterilisationRecordsExample();
        WbSterilisationRecordsExample.Criteria criteria = example.createCriteria();
        criteria.andSettlementNumberEqualTo(settlementNumber.trim());
        List<WbSterilisationRecords> list = wbSterilisationRecordsMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 根据结算编号查询 订单冲销明细
     *
     * @param settlementNumber
     * @param isOrderCodeGroupBy 是否已订单编号分组查询冲销记录  1  分组(默认) 2 不分组
     * @return
     */
    @Override
    public List<WbShippingOrder> findOrderBySettlementNumber(String settlementNumber, Integer isOrderCodeGroupBy) {
        if (ObjectUtils.isEmpty(isOrderCodeGroupBy)) {
            return wbSterilisationRecordsMapper.findOrderBySettlementNumber(settlementNumber);
        } else if (isOrderCodeGroupBy == 1) {
            return wbSterilisationRecordsMapper.findOrderBySettlementNumber(settlementNumber);
        } else if (isOrderCodeGroupBy == 2) {
            return wbSterilisationRecordsMapper.findRecordsInfoBySettlementNumber(settlementNumber);
        } else {
            return new ArrayList<WbShippingOrder>();
        }
    }

    @Override
    @Transactional
    public int insertBatch(List<WbSterilisationRecords> list) {
        //冲销
        int number = wbSterilisationRecordsMapper.insertBatch(list);
        if (number > 0) {
            for (WbSterilisationRecords wbSterilisationRecords : list) {
                String orderCode = wbSterilisationRecords.getOrderCode();
                String sterilisationAmount = wbSterilisationRecordsMapper.findRecordsByOrderCode(orderCode);
                //冲销完成自动修改状态
                wbShippingOrderService.sterilisation(sterilisationAmount, orderCode);
            }
        }
        return number;
    }

    @Transactional
    @Override
    public int insert(WbSterilisationRecords wbSterilisationRecords) {
        return wbSterilisationRecordsMapper.insert(wbSterilisationRecords);
    }

    @Transactional
    @Override
    public int delete(Long wbSterilisationRecordsId) {
        return wbSterilisationRecordsMapper.deleteByPrimaryKey(wbSterilisationRecordsId);
    }

    @Transactional
    @Override
    public int update(WbSterilisationRecords wbSterilisationRecords) {
        return wbSterilisationRecordsMapper.updateByPrimaryKeySelective(wbSterilisationRecords);
    }

    @Override
    public WbSterilisationRecords selectById(Long wbSterilisationRecordsId) {
        return wbSterilisationRecordsMapper.selectByPrimaryKey(wbSterilisationRecordsId);
    }

    @Override
    public AjaxResponse selectPage(WbSterilisationRecords wbSterilisationRecords, Integer page, Integer limit) {
        WbSterilisationRecordsExample example = new WbSterilisationRecordsExample();
        if (!ObjectUtils.isEmpty(page)) {
            example.setPageIndex((page - 1) * limit);
            example.setPageSize(limit);
        }
        WbSterilisationRecordsExample.Criteria criteria = example.createCriteria();

        List<WbSterilisationRecords> list = wbSterilisationRecordsMapper.selectByExample(example);
        Long count = wbSterilisationRecordsMapper.countByExample(example);
        return AjaxResponse.success("ok", list, count, page);
    }

    /**
     * 根据核销记录主键ID   删除销售记录
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public int deleteRecords(Long id) {
        WbSterilisationRecords wbSterilisationRecords = wbSterilisationRecordsMapper.selectByPrimaryKey(id);
        WbShippingOrderExample example = new WbShippingOrderExample();
        example.createCriteria().andOrderCodeEqualTo(wbSterilisationRecords.getOrderCode().trim());
        WbShippingOrder order = new WbShippingOrder();
        order.setStates(2);
        //修改订单状态
        wbShippingOrderMapper.updateByExampleSelective(order, example);
        return wbSterilisationRecordsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public String selectByOrderCode(String orderCode) {
        WbSterilisationRecordsExample example = new WbSterilisationRecordsExample();
        example.createCriteria().andOrderCodeEqualTo(orderCode.trim());
        List<WbSterilisationRecords> wbSterilisationRecordsList =  wbSterilisationRecordsMapper.selectByExample(example);
        Double sterilisationAmount = 0.00;
        for(WbSterilisationRecords wbSterilisationRecords:wbSterilisationRecordsList){
            sterilisationAmount = NumberUtils.add(sterilisationAmount, Double.parseDouble(wbSterilisationRecords.getSterilisationAmount().trim()));
        }
        return sterilisationAmount.toString();
    }
}
